package br.com.emanuelvictor.architectureandroid.app1.fragment;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import br.com.emanuelvictor.architectureandroid.R;
import br.com.emanuelvictor.architectureandroid.app1.entity.Daily;
import br.com.emanuelvictor.architectureandroid.app1.repository.PointRepository;
import br.com.emanuelvictor.architectureandroid.app1.support.DatabaseHelper;
import br.com.emanuelvictor.architectureandroid.app1.util.DateFormatter;
import br.com.emanuelvictor.architectureandroid.app1.util.PointVariables;
import br.com.emanuelvictor.architectureandroid.util.Interface;
import br.com.emanuelvictor.architectureandroid.util.Navigation;

@SuppressLint("SimpleDateFormat")
public abstract class EditPointFragment extends Fragment implements
		Interface {

	protected Daily dailyHandler;
	protected boolean isEditing;

	protected Calendar calendar = null;

	protected TextView firstMorningHourTextView = null;
	protected TextView secondMorningHourTextView = null;
	protected TextView morningDifferenceTextView = null;
	protected TextView firstAfternoonHourTextView = null;
	protected TextView secondAfternoonHourTextView = null;
	protected TextView afternoonDifferenceTextView = null;
	protected TextView dayDifferenceTextView = null;
	protected TextView date = null;
	protected View view = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.point_edit_item, null);
		setHasOptionsMenu(true);
		this.update();
		return view;
	}

	public void references() {
		firstMorningHourTextView = (TextView) view
				.findViewById(R.id.first_morning);
		secondMorningHourTextView = (TextView) view
				.findViewById(R.id.second_morning);
		morningDifferenceTextView = (TextView) view
				.findViewById(R.id.morning_difference);
		firstAfternoonHourTextView = (TextView) view
				.findViewById(R.id.first_afternoon);
		secondAfternoonHourTextView = (TextView) view
				.findViewById(R.id.second_afternoon);
		afternoonDifferenceTextView = (TextView) view
				.findViewById(R.id.afternoon_difference);
		dayDifferenceTextView = (TextView) view
				.findViewById(R.id.day_difference);
		date = (TextView) view.findViewById(R.id.dateedit);

		firstMorningHourTextView.setText(R.string.empty_hour);
		secondMorningHourTextView.setText(R.string.empty_hour);

		morningDifferenceTextView.setText(R.string.balance);
		firstAfternoonHourTextView.setText(R.string.empty_hour);
		secondAfternoonHourTextView.setText(R.string.empty_hour);
		afternoonDifferenceTextView.setText(R.string.balance);
		dayDifferenceTextView.setText(R.string.empty_hour);
		date.setText(R.string.date);

	}

	@Override
	public void update() {
		this.calendar = ((PointVariables)((Navigation)getActivity()).getVariables()).getHistoryNavigation().getCurrent();
		this.references();
		java.text.DateFormat df = java.text.DateFormat.getDateInstance();

		this.date
				.setText((Calendar.getInstance().get(Calendar.DAY_OF_YEAR) == this.calendar
						.get(Calendar.DAY_OF_YEAR)) ? (df.format(this.calendar
						.getTime()) + " (Hoje)") : df.format(this.calendar
						.getTime()));

		this.dailyHandler = new Daily(new PointRepository(new DatabaseHelper(
				getActivity())), calendar);

		SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");

		// Morning
		if (this.dailyHandler.getMorningFirstHour() != null) {
			this.firstMorningHourTextView.setText(dateFormat
					.format(this.dailyHandler.getMorningFirstHour().getTime()));
		}
		if (this.dailyHandler.getMorningSecondHour() != null) {
			this.secondMorningHourTextView
					.setText(dateFormat.format(this.dailyHandler
							.getMorningSecondHour().getTime()));
		}
		if (this.dailyHandler.getMorningDifference() != 0) {
			this.morningDifferenceTextView.setText(getString(R.string.balance)
					+ " "
					+ DateFormatter.fromMilis(this.dailyHandler
							.getMorningDifference()));
			this.morningDifferenceTextView.setVisibility(TextView.VISIBLE);
			if (this.dailyHandler.getMorningDifference() < 0)
				this.morningDifferenceTextView.setTextColor(getResources()
						.getColor(R.color.red));
			else
				this.morningDifferenceTextView.setTextColor(getResources()
						.getColor(R.color.green));
		} else {
			this.morningDifferenceTextView.setVisibility(TextView.INVISIBLE);
		}
		// Afternoon
		if (this.dailyHandler.getAfternoonFirstHour() != null) {
			this.firstAfternoonHourTextView
					.setText(dateFormat.format(this.dailyHandler
							.getAfternoonFirstHour().getTime()));
		}
		if (this.dailyHandler.getAfternoonSecondHour() != null) {
			this.secondAfternoonHourTextView.setText(dateFormat
					.format(this.dailyHandler.getAfternoonSecondHour()
							.getTime()));
		}
		if (this.dailyHandler.getAfternoonDifference() != 0) {
			this.afternoonDifferenceTextView
					.setText(getString(R.string.balance)
							+ " "
							+ DateFormatter.fromMilis(this.dailyHandler
									.getAfternoonDifference()));
			this.afternoonDifferenceTextView.setVisibility(TextView.VISIBLE);

			if (this.dailyHandler.getAfternoonDifference() < 0)
				this.afternoonDifferenceTextView.setTextColor(getResources()
						.getColor(R.color.red));
			else
				this.afternoonDifferenceTextView.setTextColor(getResources()
						.getColor(R.color.green));
		} else {
			this.afternoonDifferenceTextView.setVisibility(TextView.INVISIBLE);
		}
		// Day
		if (this.dailyHandler.isFullDay()) {
			this.dayDifferenceTextView.setText(DateFormatter
					.fromMilis(this.dailyHandler.getDayDifference()));
			if (this.dailyHandler.getDayDifference() < 0)
				this.dayDifferenceTextView.setTextColor(getResources()
						.getColor(R.color.red));
			else
				this.dayDifferenceTextView.setTextColor(getResources()
						.getColor(R.color.green));
			if (dailyHandler.getDayDifference() > 1800000
					|| dailyHandler.getDayDifference() < -1800000)
				Toast.makeText(this.getActivity(), R.string.dont_forget_form,
						Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		try {
			this.configureActionBar();
		} catch (Exception e) {
			e.printStackTrace();
		}
		inflater.inflate(R.menu.menueditfragment, menu);
	}



}